<template>
	<view class="add">
		<form @submit="onSubmit">
			<div class="item">
				<input v-model="formValue.title" type="text" name="title" placeholder="请输入标题" />
			</div>
			<div class="item" v-show="false">
				<input v-model="formValue.author" type="text" name="author" placeholder="请输入作者" />
			</div>
			<div class="item">
				<textarea v-model="formValue.content" name="content" placeholder="请输入详细内容" />
			</div>
			<div class="item">
				<uni-file-picker 
					v-model="imageValue"  
					file-mediatype="image"
					mode="grid"	
					@success="success" 
				/>
			</div>
			<view class="item">
				<button @click="onSet(id)">修改</button>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imageValue:[],
				icon:[],
				formValue: {
					title: '',
					author: '',
					content: ''
				},
				id: 0
			}
		},
		onLoad(e) {
			this.setData(e)
			this.id = e.id
		},
		methods: {
			// 上传成功
			success(e) {
				this.icon= e.tempFilePaths
				console.log(this.icon)
			},
			//获取要修改的数据
			setData(id) {
				uniCloud.callFunction({
					name: 'setData',
					data: id,
				}).then(res => {
					this.formValue = res.result.data[0]
					//map()	循环，返回一个对象数组
					if(!this.formValue.Image) return
					let imag_url=this.formValue.Image.map(item=>{
						return {url:item}
					})
					this.imageValue=imag_url
					console.log(this.formValue)
				})

			},
			//修改数据
			onSet(e) {
				let icon_old=this.imageValue.map(item=>{
					return item.url
				})
				uniCloud.callFunction({
					name: 'onSet',
					data: {
						id: e,
						formValue: this.formValue,
						icon:icon_old
					}
				}).then(res => {
					uni.showToast({
						icon: 'success',
						title: '修改成功'
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 2
						});
					}, 800)
				})

			}
		},
		computed: {
			//提交按钮禁用
			flag() {
				
				return this.formValue.author == '' ||
					this.formValue.content == '' ||
					this.formValue.title == '' ? true : false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		padding: 30rpx;

		.item {
			padding-bottom: 20rpx;

			input {
				border: 1px solid #eee;
				height: 80rpx;
				padding: 0 20rpx;
			}

			textarea {
				border: 1px solid #eee;
				width: 100%;
				padding: 0 20rpx;
				box-sizing: border-box;
			}

			button {
				margin-top: 10rpx;
			}
		}
	}
</style>